package com.ruoyi.project.zxcrm.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.zx.PageData;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.domain.SysConfig;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.mapper.SysConfigMapper;
import com.ruoyi.project.system.service.ISysDictTypeService;
import com.ruoyi.project.zxcrm.mapper.CrmLeadMapper;
import com.ruoyi.project.zxcrm.mapper.CrmSmsSendLogMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 订单跟踪Service业务层处理
 *
 * @author wenrui
 * @date 2022-12-30
 */
@Service
@RequiredArgsConstructor
@Transactional
public class CrmLeadServiceImpl extends BaseController {

    private final CrmLeadMapper crmLeadMapper;

    private final SysConfigMapper sysConfigMapper;

    /**
     * 查询订单跟踪
     *
     * @param id 订单跟踪主键
     * @return 订单跟踪
     */

    public PageData selectCrmLeadById(Long id) {
        return crmLeadMapper.selectCrmLeadById(id);
    }

    /**
     * 查询订单跟踪列表
     *
     * @param pd 订单跟踪
     * @return 订单跟踪
     */

    public List<PageData> selectCrmLeadList(PageData pd) {
        List<PageData> list = crmLeadMapper.selectCrmLeadList(pd);
        return list;
    }

    /**
     * 新增订单跟踪
     *
     * @param pd 订单跟踪
     * @return 结果
     */

    public int insertCrmLead(PageData pd) {
        if (pd.get("next_time") == null) {
            SysConfig config = sysConfigMapper.checkConfigKeyUnique("crm_create_lead_next_time");
            String day = config.getConfigValue();
            Date date = DateUtils.addDays(DateUtils.getNowDate(), Integer.parseInt(day));
            pd.put("next_time", date);
        }
        pd.put("type", "1");
        pd.put("state", "0");
        pd.put("update_by", this.getUsername());
        pd.put("create_by", this.getUsername());
        pd.put("user_id", this.getUserId());
        pd.put("remark", "初次导入");
        PageData pdComment = new PageData();
        pdComment.put("id", pd.getLong("id"));
        pdComment.put("crm_state", pd.getString("state"));
        pdComment.put("remark", "初次导入");
        this.insertLeadComment(pdComment);
        return crmLeadMapper.insertCrmLead(pd);
    }

    public int updateCrmLead(PageData pd) {
        PageData pageData = crmLeadMapper.selectCrmLeadById(pd.getLong("id"));
        pd.put("update_by", this.getUsername());
        pd.put("update_time", DateUtils.getNowDate());
        if (pageData == null) {
            return crmLeadMapper.insertCrmLead(pd);
        } else {
            return crmLeadMapper.updateCrmLead(pd);
        }
    }

    /**
     * 批量删除订单跟踪
     *
     * @param ids 需要删除的订单跟踪主键
     * @return 结果
     */

    public int deleteCrmLeadByIds(Long[] ids) {
        return crmLeadMapper.deleteCrmLeadByIds(ids);
    }

    /**
     * 删除订单跟踪信息
     *
     * @param id 订单跟踪主键
     * @return 结果
     */

    public int deleteCrmLeadById(Long id) {
        return crmLeadMapper.deleteCrmLeadById(id);
    }


    public List<PageData> selectCrmLeadCommentList(PageData pd) {
        List<PageData> pageData = crmLeadMapper.selectCrmLeadCommentList(pd);
        return pageData;
    }


    public int insertLeadComment(PageData pd) {
        pd.put("create_by", this.getUsername());
        pd.put("tenant_id", this.getUserTenantId());
        pd.put("create_time", DateUtils.getNowDate());
        return crmLeadMapper.insertLeadComment(pd);
    }

}
